<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/10
 * Time: 10:13
 */

namespace app\api\controller\v2;





use app\api\lib\exception\MissException;
use app\api\lib\exception\ParamException;
use app\api\lib\exception\SuccessMessage;
use app\api\service\TokenService;
use app\api\validate\Appointment;
use app\api\validate\BaseValidate;
use app\api\validate\IdMustInt;

class AppointmentController extends BaseController
{
    public function _initialize()
    {
        $this->checkLogin();
        $this->uid = TokenService::getCurrentUid();
    }

    //获取用户自己的所有预约
    public function getAppointmentList()
    {
        //待确认 店家打电话确认修改为 已确认 到店吃饭预约完成
        throw new SuccessMessage([
            'data' =>\app\api\model\Appointment::where('uid',$this->uid)->where('state','neq',4)->select(),
        ]);
    }

    //取消订单
    public function cancelAppointment()
    {
        $data = input('post.');
        $result = $this->validate($data,'Appointment.cancel');
        if(true !== $result){
            throw new ParamException([
                'msg'=>$result,
            ]);
        }
        $result = \app\api\model\Appointment::get(input('id/d'));
        if($result['uid'] != $this->uid){
            throw new MissException();
        }
        if(in_array($result['state'],['已完成','商家受理中','订单关闭'])){
            throw new MissException();
        }
        $result->save(['reason' => $data['reason'],'state' => 3],['id'=>$data['id']]);
        throw new SuccessMessage();
    }

    //预约详情
    public function getInfo($id)
    {
        (new IdMustInt())->goCheck();
        $result = \app\api\model\Appointment::where(['uid' => $this->uid,'id' => $id])->find();
        if($result['uid'] != $this->uid){
            throw new MissException();
        }
        if(empty($result)){
            throw new MissException();
        }
        return $result->hidden(['id']);
    }

    //添加预约
    public function createAppointment()
    {
        $data = input('post.');
        $result = $this->validate($data,'Appointment.add');
        if(true !== $result){
            throw new ParamException([
                'msg'=>$result,
            ]);
        }
        (new BaseValidate())->getDataByRule($data);
        (new BaseValidate())->getDataIDByRule($data);
        if(\app\api\model\Appointment::where('uid',$this->uid)->where('state','in',[0,1])->count() >= 3){
            throw new MissException([
                'msg' => '您的预约太多了',
            ]);
        }
        $time = explode(':',$data['time']);
        $userList = \app\admin\model\Appointment::where(['uid' => $this->uid,'state'=>['in',[0,1]]])->select();
        foreach ($userList as $k => $v){
            if($time[0] < explode(':',$v['time'])[0] +1 && $time[0] > explode(':',$v['time'])[0] -1){
                throw new MissException([
                    'msg' => '您的两次预约时间太近了',
                ]);
            }
        }
        $data = input('post.');
        $data['uid'] = $this->uid;
        $appointment = new \app\api\model\Appointment();
        $appointment->allowField(true)->save($data);
        return new SuccessMessage();
    }



}